package comWorld.blogCatalog.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import comWorld.blog.BlogInfoManage;
import comWorld.blogCatalog.BlogCatalog;
import comWorld.blogCatalog.BlogCatalogManage;
import comWorld.blogCatalog.BlogCatalogs;
import comWorld.user.UserInfo;

/**
 * Servlet implementation class BlogCatalogsPageInit
 */
@WebServlet("/admin/blog-catalogs")
public class BlogCatalogsPageInit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BlogCatalogsPageInit() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserInfo visitorInfo = (UserInfo) request.getSession().getAttribute("visitorInfo");
		BlogCatalogs blogCatalogs = new BlogCatalogs(visitorInfo.getId());
		blogCatalogs.statisticsBlogCount(visitorInfo.getId(), 0);
		request.setAttribute("blogCatalogs", blogCatalogs.getCatalogs());
		String catalogIdStr = request.getParameter("edit_catalog");
		int catalogId = 0;
		if (catalogIdStr != null && catalogIdStr.matches("^\\d+$")) {
			BlogCatalog catalog = BlogCatalogManage.getBlogCatalog(Integer.valueOf(catalogIdStr));
			if (catalog != null) {
				request.setAttribute("blogCatalog", catalog);
				catalogId = catalog.getCatalogId();
			}
		}
		request.setAttribute("catalogId", catalogId);
		String forward = "/user/spaceAdmin/blogCatalogs.jsp";
		if (catalogId != 0) 
			forward = "/user/spaceAdmin/editBlogCatalog.jsp";
		request.getRequestDispatcher(forward).forward(request, response);
	}

}
